function ListNode(val, next) {
  this.val = (val===undefined ? 0 : val)
  this.next = (next===undefined ? null : next)
}
function createList(arr) {
  if(arr.length == 0)
    return null;
  let head = new ListNode(arr[0], null), headNode = head;
  for(let i = 1; i < arr.length; i++) {
    let temp = new ListNode(arr[i], null);
    head.next = temp;
    head = temp;
  }
  return headNode;
}
function showList(head) {
  while(head) {
    console.log(head.val);
    head = head.next;
  }
}
/**
 * @param {ListNode} list1
 * @param {ListNode} list2
 * @return {ListNode}
 */
var mergeTwoLists = function(list1, list2) {
  // let list1 = createList(List1), list2 = createList(List2);
  if(list1 == null && list2 == null) {
    return null;
  } else if(list1 == null) {
    return list2;
  } else if(list2 == null) {
    return list1;
  }
  let nowNode, headerNode;
  if(list1.val <= list2.val) {
    nowNode = new ListNode(list1.val, null);
    headerNode = nowNode;
    list1 = list1.next;
  } else {
    nowNode = new ListNode(list2.val, null);
    headerNode = nowNode;
    list2 = list2.next;
  }
  while(list1 && list2) {
    let temp;
    if(list1.val <= list2.val) {
      temp = new ListNode(list1.val, null);
      list1 = list1.next;
    } else {
      temp = new ListNode(list2.val, null);
      list2 = list2.next;
    }
    nowNode.next = temp;
    nowNode = temp;
  }
  if(list1) {
    nowNode.next = list1;
  }
  if(list2) {
    nowNode.next = list2;
  }
  // showList(headerNode);
  return headerNode;
};
let l1 = [1,2,4], l2 = [1,3,4];
console.log(mergeTwoLists(l1, l2));
l1 = [], l2 = [];
console.log(mergeTwoLists(l1, l2));
l1 = [], l2 = [0];
console.log(mergeTwoLists(l1, l2));

